package info.maxhis.bn;

import info.maxhis.bn.birthday.SearchBirthdayActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 在此类中实现系统菜单
 * @author iStar
 *
 */
public class BaseActivity extends Activity {
	
	private static final int MENU_SETTING = 0;
	private static final int MENU_ABOUT = 1;
	private Context context;
	private Resources rs;      

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		rs = getResources();  
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_SETTING, 0, R.string.setting).setIcon(android.R.drawable.ic_menu_preferences);
		menu.add(0, MENU_ABOUT, 0, R.string.about).setIcon(android.R.drawable.ic_menu_info_details);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent();
		switch (item.getItemId()) {

		case MENU_SETTING:
			intent.setClass(this, SettingsActivity.class);
			startActivity(intent);
			break;
			
		case MENU_ABOUT:
			LayoutInflater inflater = LayoutInflater.from(context);
			RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.about, null);
			TextView t1 = (TextView)layout.findViewById(R.id.about_me1);
			TextView t2 = (TextView)layout.findViewById(R.id.about_me2);
			TextView t3 = (TextView)layout.findViewById(R.id.about_me3);
			TextView t4 = (TextView)layout.findViewById(R.id.about_me4);
			TextView t5 = (TextView)layout.findViewById(R.id.about_me5);
			TextView t6 = (TextView)layout.findViewById(R.id.about_me6);
			TextView t7 = (TextView)layout.findViewById(R.id.about_me7);
			t1.setMovementMethod(LinkMovementMethod.getInstance());
			t2.setMovementMethod(LinkMovementMethod.getInstance());
			t3.setMovementMethod(LinkMovementMethod.getInstance());
			t4.setMovementMethod(LinkMovementMethod.getInstance());
			t5.setMovementMethod(LinkMovementMethod.getInstance());
			t6.setMovementMethod(LinkMovementMethod.getInstance());
			t7.setMovementMethod(LinkMovementMethod.getInstance());
			AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			dialog.setView(layout);
	    	dialog.setIcon(R.drawable.icon).setTitle(rs.getString(R.string.about));
			dialog.setNegativeButton(rs.getString(R.string.close),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
						}
			});
			dialog.create().show();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			 //按查找键进入查找好友页面
			Intent intent = new Intent(context, SearchBirthdayActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
		 }
		return super.onKeyDown(keyCode, event);
	}
}
